package com.zlzvip.android.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.zlzvip.android.R;
import com.zlzvip.android.account.IndexPicEntity;
import com.zlzvip.android.account.UrlCenter;

public class MyFirstActivity extends BaseActivity {
    Intent intent;
    Runnable runnable;
    Handler handler = new Handler();
    String url;
    String h5Link;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        intent = new Intent();
        initData();

    }

    private void initData() {
        HttpRequest request = new HttpRequest(MyFirstActivity.this);
        request.get(UrlCenter.SETPIC, null, new DataRequestListener<IndexPicEntity>(IndexPicEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(final IndexPicEntity o) {
                super.onSuccess(o);
                if (TextUtils.isEmpty(o.getAppStartup().getPhoto())) {
                    url="";
                } else {
                    url=o.getAppStartup().getPhoto();

                    h5Link = o.getAppStartup().getH5Link();
                }
                initActivity();
            }

            @Override
            public void onDataParseError(Exception e) {
                super.onDataParseError(e);
                url="";
                initActivity();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                url="";
                initActivity();
            }
        });




    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_first,null);
    }

    private void initActivity() {
        runnable = new Runnable() {
            public void run() {
                    if(TextUtils.isEmpty(url)){
                        intent.setClass(MyFirstActivity.this, MainActivity.class);
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        startActivity(intent);
                        finish();
                    }else{
                        intent.setClass(MyFirstActivity.this, MySplashActivity.class);
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                        intent.putExtra("url",url);
                        intent.putExtra("h5Link",h5Link);

                        startActivity(intent);
                        finish();
//                       MySplashActivity.start(MyFirstActivity.this,url);
                    }

            }
        };
        handler.postDelayed(runnable, 3000);
    }


}
